<?php

namespace app\api\controller;

use app\api\model\Article as ArticleModel;
use app\api\model\article\Category as CategoryModel;
use \think\Db;

/**
 * 活动推广控制器
 * Class Article
 * @package app\api\controller
 */
class Promotion extends Controller
{
    public $userInfo = [];

    private function get_user_info(){
        $token = $this->request->param('token');
        if($token){
            $userInfo = $this->getUser();
            if($userInfo && !is_array($userInfo)){
                $userInfo = $userInfo->toArray();
            }
        }else{
            $openid = $this->request->param('openid');
            $userInfo = Db::name('user')->where('open_id',$openid)->find();
        }
        

        $this->userInfo = $userInfo;
    }

	public function activity(){
        $this->get_user_info();
		$pid = request()->param('pid');
        if(!$pid){
            return $this->renderError('参数错误');
        }
        $promotion = Db::name('activity_promotion')->where('id',$pid)->find();
        if(!$promotion){
            return $this->renderError('推广信息不存在！');
        }
        $end_time = strtotime($promotion['end_time']);
        if($end_time<time()){
            return $this->renderError('推广已结束！');
        }

        Db::name('activity_promotion')->where('id', $pid)->setInc('view');

        $info = Db::name('activity')->where('id',$promotion['aid'])->where('status',1)->find();
        if(!$info){
            return $this->renderError('活动不存在或已结束！');
        }

        $kefu = [
            'url'=>'https://work.weixin.qq.com/kfid/kfc40341b633d7a8607',
            'corpId'=>'wwa0862bb492a3dc82',
        ];

        // Db::name('activity')->where('id', $id)->setInc('view');
        $this->assign('promotion',$promotion);
        $this->assign('info',$info);
		return $this->renderSuccess(compact('promotion','info','kefu'));
	}

    public function statistics(){
        $this->get_user_info();

        $pid = request()->param('pid');
        $promotion = Db::name('activity_promotion')->where('id',$pid)->find();
        if(!$promotion){
            return $this->rendersuccess('推广信息不存在！');
        }

        $visitor_os = '';

        //统计表ID
        $id = request()->param('id');

        $userAgent = $_SERVER['HTTP_USER_AGENT'];


        
        if($id>0 && request()->isPost()){
            $last = Db::name('activity_promotion_statistics')->where('id',$id)->find();
            if(!$last){
                return $this->renderSuccess('error');
            }

            Db::name('activity_promotion_statistics')->where('id',$id)->update([
                'pid'=>$pid,
                'aid'=>$promotion['aid'],
                'uid'=>$this->userInfo['user_id'],
                'page_duration'=>time()-$last['visit_time'],
                'page_url'=>request()->param('currentUrl'),
                'referrer_url'=>request()->param('referrer_url'),
                'last_ip'=>request()->ip(),
                'visitor_ip'=>request()->ip(),
                'leave_time'=>time(),
                'user_agent'=>$userAgent,
                'screen'=>request()->param('screen'),
                //'visit_count' => Db::raw('visit_count+1'),
            ]);
            return $this->renderSuccess('首次');
        }else{
            if(!$id){
                $stat = Db::name('activity_promotion_statistics')->where([
                    'pid'=>$pid,
                    'aid'=>$promotion['aid'],
                    'uid'=>$this->userInfo['user_id'],
                    'sid'=>0
                ])->order('id desc')->find();
                if($stat){
                    $sid = $stat['sid']>0?$stat['sid']:0;
                    Db::name('activity_promotion_statistics')->where('id',$stat['id'])->whereOr('sid',$sid)->update([
                        'last_time' => date('Y-m-d H:i:s'), //最后一次访问时间
                        'visit_count' => Db::raw('visit_count+1'),
                    ]);
                }

                    $id = Db::name('activity_promotion_statistics')->insertGetId([
                        'pid'=>$pid,
                        'sid'=>isset($stat['id'])?$stat['id']:0,
                        'aid'=>$promotion['aid'],
                        'uid'=>$this->userInfo['user_id'],
                        'visit_time'=>time(),
                        'page_duration'=>1,
                        'ip'=>request()->ip(),
                        'leave_time'=>0,
                        'last_time'=>date('Y-m-d H:i:s'),
                        'visitor_ip'=>request()->ip(),
                        'last_ip'=>request()->ip(),
                        'visitor_browser'=>'',
                        'visitor_os'=>$visitor_os,
                        'page_url'=>$_SERVER['HTTP_REFERER'],
                        'referrer_url'=>'',
                        'visit_count'=>1,
                        'user_agent'=>$userAgent,
                    ]);
            }
        }
        return $this->renderSuccess('success');


    }
}
